#include "extras/utils.h"


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

#include <GL/glew.h>
#include <GL/glut.h>

void Utils::drawBox(bool filled, float minX, float minY, float minZ,
		float maxX, float maxY, float maxZ) {

	if (filled) {
		glBegin(GL_QUADS);
	} else {
		glBegin(GL_LINE_LOOP);
	}
	//bottom
	glVertex3f(minX, minY, minZ);
	glVertex3f(maxX, minY, minZ);
	glVertex3f(maxX, minY, maxZ);
	glVertex3f(minX, minY, maxZ);

	if (!filled) {
		glEnd();
		glBegin(GL_LINE_LOOP);
	}

	//top
	glVertex3f(minX, maxY, maxZ);
	glVertex3f(maxX, maxY, maxZ);
	glVertex3f(maxX, maxY, minZ);
	glVertex3f(minX, maxY, minZ);

	glEnd();
	// draw sides

	if (filled) {
		glBegin(GL_QUAD_STRIP);
	} else {
		glBegin(GL_LINES);
	}

	glVertex3f(maxX, maxY, maxZ);
	glVertex3f(maxX, minY, maxZ);

	glVertex3f(maxX, maxY, minZ);
	glVertex3f(maxX, minY, minZ);

	glVertex3f(minX, maxY, minZ);
	glVertex3f(minX, minY, minZ);

	glVertex3f(minX, maxY, maxZ);
	glVertex3f(minX, minY, maxZ);

	glVertex3f(maxX, maxY, maxZ);
	glVertex3f(maxX, minY, maxZ);

	glEnd();

}

void Utils::printText(char *s, int x, int y, int lineSpacing, int letterSpacing){

	int xorg = x;

	for (char *c = s; *c != '\0'; c++) {

		if(*c == '\n'){
			x = xorg;
			y += lineSpacing;
		}else{
			glRasterPos2f(x, y);
			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c);
			x += glutBitmapWidth(GLUT_BITMAP_HELVETICA_12, *c) + letterSpacing;
		}
	}
}

void Utils::printGLError(const char* location){

	 if(glGetError() != GL_NO_ERROR){

		 printf("The OpenGL error: %s , was encountered in %s\n",
				 gluErrorString(glGetError()), location);

	}

}

